package org.liuhy.jbpm4
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	
	import org.CacheUtils;
	import org.liuhy.FlowPanel;
	import org.liuhy.jbpm4.property.ProcessProperty;
	import org.liuhy.jbpm4.utils.JbpmRectLineUtils;
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.line.LineNode;
	import org.liuhy.node.FlowNode;
	import org.liuhy.node.focus.FocusNode;
	import org.liuhy.utils.FlowNodeUtils;
	//流程设计器，设计面板
	public class JbpmFlowPanel extends FlowPanel
	{
		private var _property:ProcessProperty = null;
		public function JbpmFlowPanel()
		{
			super();
			_property = new ProcessProperty(this);
		}
		override public function onDragDrop(event:DragEvent):void{
			if(event.dragInitiator is FocusNode){
				var _focusNode:FocusNode = event.dragInitiator as FocusNode;
				_focusNode.adjustFlowNode(event);
			}else if(event.dragInitiator is LineNode){
				var _lineNode:LineNode = event.dragInitiator as LineNode;
				_lineNode.lineNodeDrop(event);
			}else if(event.dragInitiator is Image){
				//
				CacheUtils.push(this.exportXML());
				var _img:Image = event.dragInitiator as Image;
				var _graphic:UIComponent = JbpmGraphicFactory.createNEW(_img.id,new Point(event.localX,event.localY));
				this.addChild(_graphic);				
			}else if(event.dragInitiator is FlowNode){
				var _flowNode:FlowNode = event.dragInitiator as FlowNode;
				_flowNode.flowNodeDrop(event);
			}
		}
		override public function onCanvasClick(event:MouseEvent):void{
			super.onCanvasClick(event);
			var _vbox:VBox = this.parent as VBox;
			var _c:Canvas = _vbox.getChildAt(1) as Canvas;
			var _cArray:Array = _c.getChildren();
			for(var i:int=0;i<_cArray.length;i++){
				(_cArray[i] as Canvas).visible = false;
			}
			if(!_c.contains(_property)){
				_c.addChild(_property);
			}else{
//				_property.expr = this._expr;
			}
			_property.visible = true;
			_c.setChildIndex(_property,_c.numChildren-1);		
		}

		override public function importXML(p_xml:String):void{
			var _xml:XML = new XML(p_xml);
			_property.title = _xml.@name.toString();
			var _flowNodeList:ArrayCollection = new ArrayCollection();
			//创建节点;
			var _nodeList:XMLList = _xml.children();
			for(var i:int=0;i<_nodeList.length();i++){
				var _nodeXML:XML = _nodeList[i] as XML;
				var _flowNode:FlowNode = JbpmGraphicFactory.getFlowNodeByXML(_nodeXML);
				_flowNode.draw();
				this.addChild(_flowNode);
				_flowNodeList.addItem(_flowNode);
			}
			for(var i:int=0;i<_nodeList.length();i++){
				var _nodeXML:XML 			= _nodeList[i] as XML;
				var _flowNodeName 			= _nodeXML.@name;
				var _tranXMLList:XMLList	= _nodeXML.children();
				var _flowNode:FlowNode 		= FlowNodeUtils.getFlowNodeByName(_flowNodeName,_flowNodeList);

				//构建链接线;
				for(var j:int=0;j<_tranXMLList.length();j++){
					var _tranXML:XML = _tranXMLList[j] as XML;
					var _cLine:CLine = new CLine();
					this.addChild(_cLine);
					var _g:String 	= _tranXML.attribute("g").toString();	//线的点坐标;
					var _to:String	= _tranXML.attribute("to").toString();						
					var _lineArray:ArrayCollection = JbpmRectLineUtils.convert2LineArray(_g,_cLine);
					if(null!=_lineArray && _lineArray.length>0){
						//出线;
						var _bLine:AbstractLine = (_lineArray.getItemAt(0) as AbstractLine);
						var _outFocus:FocusNode = _flowNode.getGlobalNearFocusNode(new Point(_cLine.x+_bLine.x1,_cLine.y+_bLine.y1));
						_bLine.out_flowFocusNode = _outFocus;
						_flowNode.addOutLine(_bLine);
						//入线;
						var _iLine:AbstractLine = (_lineArray.getItemAt(_lineArray.length-1) as AbstractLine);
						if(null!=_to && ""!=_to){
							var _toFlowNode:FlowNode = FlowNodeUtils.getFlowNodeByName(_to,_flowNodeList);
							var _inFocus:FocusNode = _toFlowNode.getGlobalNearFocusNode(new Point(_cLine.x+_iLine.x2,_cLine.y+_iLine.y2));
							_iLine.in_flowFocusNode = _inFocus;;
							_toFlowNode.addInLine(_iLine);
						}
					}
					_cLine.title=_tranXML.@name.toString();
					_cLine.adjustTitle(null);
				}
			}
		}
		
		//文本框中显示图形界面后的xml内容
		override public function exportXML():String{
			var _cArray:Array = this.getChildren();
			var _cLineArray:ArrayCollection = new ArrayCollection();
			var _cFlowNodeArray:ArrayCollection = new ArrayCollection();
			var _xml:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
			var _xml_nodes:String = "<process name=\"" + _property.title + "\" xmlns=\"http://jbpm.org/4.0/jpdl\">\n";
			for(var i:int=0;i<_cArray.length;i++){
				var _obj:Object = _cArray[i];
				if(_obj is FlowNode){
					var _fn:FlowNode = _obj as FlowNode;
					_xml_nodes = _xml_nodes + _fn.toXML() + "\n";
				}
			}			
			_xml = _xml + _xml_nodes +  "</process>";
			return _xml; 
		}
	}
}